package org.example.teaView;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;
import java.util.Stack;

public class SeeStuView extends JFrame implements Runnable{
    HandleSeeAllStudent handleSeeAllStudent;
    JButton back,seeAll,search;
    JTable showResult;
    Object[] tableTitle = new Object[]{"考生ID","考生成绩"};
    Object[][] tableData = new Object[][]{

    };
    DefaultTableModel tableModel = new DefaultTableModel(tableData,tableTitle);
    JScrollPane js;
    JTextField input_ID,hintMess;


    SeeStuView(){
        init();
        seeAllTopicsListener();

    }
    private void init() {
        this.setSize(800,800);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);
        this.setTitle("查看所有学生成绩页面-3220608045吉治霖软件2202");

        //输入ID查询
        Font font = new Font(null,1,16);
        JLabel IDText = new JLabel("学生ID：");
        IDText.setFont(font);
        IDText.setBounds(50,35,110,22);
        this.getContentPane().add(IDText);

        input_ID = new JTextField();
        input_ID.setBounds(150,35,600,22);
        this.getContentPane().add(input_ID);
        //显示框
        showResult = new JTable(tableModel);
        js = new JScrollPane(showResult);
        js.setBounds(150,75,500,500);
        this.getContentPane().add(js);
        //查看所有按钮
        seeAll = new JButton("查看所有");
        seeAll.setBounds(100,600,120,30);
        this.getContentPane().add(seeAll);
        //搜索按钮
        search = new JButton("搜索");
        search.setBounds(350,600,80,30);
        this.getContentPane().add(search);
        //返回按钮
        back = new JButton("返回");
        back.setBounds(600,600,80,30);
        this.getContentPane().add(back);
        //提示框
        hintMess = new JTextField();
        hintMess.setEditable(false);
        hintMess.setBounds(50,700,700,30);
        this.getContentPane().add(hintMess);
    }

    private void seeAllTopicsListener() {
        handleSeeAllStudent = new HandleSeeAllStudent();
        handleSeeAllStudent.setSeeStuView(this);
        seeAll.addActionListener(handleSeeAllStudent);
        search.addActionListener(handleSeeAllStudent);
        back.addActionListener(handleSeeAllStudent);
    }

    @Override
    public void run() {
        String result =null;
        while (true){
            try {
                result = handleSeeAllStudent.in.readUTF();
                if (result.startsWith("学生成绩")){
                    String[] data = result.split("##");
                    tableModel.addRow(new Object[]{
                            data[1],data[2]
                    });
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
